<template>
	<div>
		<h3>公募基金文件更新</h3>
		<!-- 头部 -->
		<el-table :data="list" border style="width: 100%">
			<template v-for="(item, index) in listMsgPublic">
				<el-table-column :key="index + 'b'" prop="name" :label="item.title" :width="item.title === '更新重大事项临时报告' ? '200' : ''">
					<template :slot-scope="{ row: item }">
						<el-button :disabled="isBaseDisabled" type="primary" size="mini" @click="updateShow(item.title)">{{ item.title }}</el-button>
					</template>
				</el-table-column>
			</template>
		</el-table>

		<!-- 列表 -->
		<h3>更新列表</h3>
		<el-table :data="fileRenewApplyList" border class="clls" style="width: 100%">
			<el-table-column prop="typeName" label="名称">
				<template slot-scope="scope">
					<!-- 公募 -->
					<span v-if="scope.row.fileRenewCode === 'upfundContract'">更新基金合同文件</span>
					<span v-if="scope.row.fileRenewCode === 'uptrustAgreement'">更新托管协议文件</span>
					<span v-if="scope.row.fileRenewCode === 'upenlistExplain'">更新招募说明书文件</span>
					<span v-if="scope.row.fileRenewCode === 'upenlistExplainRemark'">更新产品资料概要</span>
					<span v-if="scope.row.fileRenewCode === 'quarterly'">更新季报</span>
					<span v-if="scope.row.fileRenewCode === 'annual'">更新年报</span>
					<span v-if="scope.row.fileRenewCode === 'matters'">更新重大事项临时报告</span>
					<span v-if="scope.row.fileRenewCode === 'liquidation'">更新清算报告</span>
					<span v-if="scope.row.fileRenewCode === 'contract'">更新更新合同</span>
					<span v-if="scope.row.fileRenewCode === 'plant'">更新计划说明书</span>
					<span v-if="scope.row.fileRenewCode === 'risk'">更新风险揭示书</span>
					<span v-if="scope.row.fileRenewCode === 'supplement'">更新补充协议</span>
					<span v-if="scope.row.fileRenewCode === 'others'">更新其他文档</span>
				</template>
			</el-table-column>
			<el-table-column prop="date" label="生效时间">
				<template slot-scope="scope">
					<div v-if="!!scope.row.firstEffectiveDate">word版：{{ scope.row.firstEffectiveDate | timerConversionStr }}</div>
					<div v-if="!!scope.row.sealedEffectiveDate">盖章版：{{ scope.row.sealedEffectiveDate | timerConversionStr }}</div>
					<div v-if="!!scope.row.finalEffectiveDate">PDF版：{{ scope.row.finalEffectiveDate | timerConversionStr }}</div>
				</template>
			</el-table-column>
			<el-table-column prop="name" label="操作">
				<template slot-scope="scope">
					<div class="items" style="border: none">
						<el-button size="mini" type="primary" @click="fileClick(scope.row, '查看')">查看</el-button>
						<el-button size="mini" type="primary" @click="fileClick(scope.row, '下载')">下载</el-button>
						<el-button
							size="mini"
							type="danger"
							:disabled="isBaseDisabled"
							class="delebtn"
							@click="fileClick(scope.row, '删除', scope.$index + 1)"
							>删除</el-button
						>
						<el-button size="mini" type="primary" :disabled="isBaseDisabled" @click="updateShow('更新', scope.row, scope.$index + 1)"
							>更新</el-button
						>
					</div>
				</template>
			</el-table-column>
		</el-table>

		<!-- 上传组件 -->
		<el-dialog :title="title" :visible.sync="updateAddComShow" width="30%" :before-close="updateCancel" :append-to-body="true">
			<el-form label-width="120px">
				<el-form-item label="生效时间：">
					<el-date-picker v-model="date1" type="date" value-format="yyyy-MM-dd" format="yyyy-MM-dd" placeholder="选择日期" />
				</el-form-item>
				<el-form-item label=" ">
					<div class="updateClass">
						<el-upload
							action="https://jsonplaceholder.typicode.com/posts/"
							:show-file-list="false"
							:http-request="toUploadFile"
							:limit="1"
							:file-list="fileList"
						>
							<el-button size="mini" type="primary" @click.native="updateClick('word版')"
								><i class="el-icon-upload" /> word版</el-button
							>
						</el-upload>
						<el-upload
							action="https://jsonplaceholder.typicode.com/posts/"
							:show-file-list="false"
							:http-request="toUploadFile"
							:limit="1"
							:file-list="fileList"
						>
							<el-button size="mini" type="primary" @click.native="updateClick('盖章版')"
								><i class="el-icon-upload" /> 盖章版</el-button
							>
						</el-upload>
						<el-upload
							action="https://jsonplaceholder.typicode.com/posts/"
							:show-file-list="false"
							:http-request="toUploadFile"
							:limit="1"
							:file-list="fileList"
						>
							<el-button size="mini" type="primary" @click.native="updateClick('PDF版')"><i class="el-icon-upload" /> PDF版</el-button>
						</el-upload>
					</div>
				</el-form-item>
			</el-form>
			<span slot="footer" class="dialog-footer">
				<el-button size="mini" type="primary" style="margin: 2px 20px 0 0" @click="updateCancel">取 消</el-button>
			</span>
		</el-dialog>

		<!-- 操作文件 -->
		<el-dialog title="文件操作" :visible.sync="addComShowFile" width="20%" :before-close="cancelFile" :append-to-body="true">
			<el-button size="mini" :disabled="!!!fileObj.firstFileId" type="primary" @click.native="fileOperationFunc('word版')"
				><i class="el-icon-upload" /> word版</el-button
			>
			<el-button size="mini" :disabled="!!!fileObj.sealedFileId" type="primary" @click.native="fileOperationFunc('盖章版')"
				><i class="el-icon-upload" /> 盖章版</el-button
			>
			<el-button size="mini" :disabled="!!!fileObj.finalFileId" type="primary" @click.native="fileOperationFunc('PDF版')"
				><i class="el-icon-upload" /> PDF版</el-button
			>
		</el-dialog>
	</div>
</template>
<script>
import { getFundFile, saveUpdateFile, delUpdateFile, UpdateFile } from '@/api/productApi/infoMain'
import { fundFileUpload, downloadFile, openFile } from '@/api/productApi/file'
import { timerConversionDate } from '@/filters'
import { jsonArray } from '../rateInfo/rateInfo'
export default {
	props: {
		isBaseDisabled: {
			type: Boolean,
			default: false
		}
	},
	data() {
		return {
			listMsgPublic: [
				{ title: '更新基金合同', fileRenewCode: 'upfundContract' },
				{ title: '更新托管协议', fileRenewCode: 'uptrustAgreement' },
				{ title: '更新招募说明书', fileRenewCode: 'upenlistExplain' },
				{ title: '更新产品资料概要', fileRenewCode: 'upenlistExplainRemark' },
				{ title: '更新其他文档', fileRenewCode: 'others' }
			],
			list: [{ name: '1' }],
			fileRenewApplyList: [],
			updateAddComShow: false,
			title: '',
			fileTypeMsg: '', // 文件上传类型
			fileObj: {},
			fileList: [],
			date1: new Date(),
			addComShowFile: false,
			fundSysCode: '',
			tabIndex: null,
			fileOperType: '' // 文件操作类型
		}
	},
	methods: {
		// 文件操作
		fileOperationFunc(type) {
			if (type === 'word版') {
				this.fileoper(this.fileObj.firstFileId, type)
			}
			if (type === '盖章版') {
				this.fileoper(this.fileObj.sealedFileId, type)
			}
			if (type === 'PDF版') {
				this.fileoper(this.fileObj.finalFileId, type)
			}
		},
		fileoper(id, type) {
			if (this.fileOperType === '下载') {
				this.downloadFileFunc(id)
			} else if (this.fileOperType === '查看') {
				this.fileSee(id)
			} else if (this.fileOperType === '删除') {
				const oldObject = jsonArray(this.fileObj)
				let newFileRenewInfo = jsonArray(this.fileObj)
				if (type === '盖章版') {
					newFileRenewInfo.sealedEffectiveDate = ''
					newFileRenewInfo.sealedFileId = ''
					newFileRenewInfo.sealedTDate = ''
					newFileRenewInfo.sealedFileName = ''
					newFileRenewInfo.sealedFileMd5 = ''
				} else if (type === 'word版') {
					newFileRenewInfo.firstEffectiveDate = ''
					newFileRenewInfo.firstFileId = ''
					newFileRenewInfo.firstTDate = ''
					newFileRenewInfo.firstFileName = ''
					newFileRenewInfo.firstFileMd5 = ''
				} else if (type === 'PDF版') {
					newFileRenewInfo.finalEffectiveDate = ''
					newFileRenewInfo.finalFileId = ''
					newFileRenewInfo.finalTDate = ''
					newFileRenewInfo.finalFileName = ''
					newFileRenewInfo.finalFileMd5 = ''
				}
				if (!newFileRenewInfo.firstFileId && !newFileRenewInfo.sealedFileId && !newFileRenewInfo.finalFileId) {
					newFileRenewInfo = null
				}
				delUpdateFile({
					fundSysCode: this.fundSysCode,
					fundRaiseType: '1',
					newFileRenewInfo,
					fileRenewInfo: oldObject,
					fileRenewId: oldObject.fileRenewId
				}).then(res => {
					if (res.msgCode === 'Y') {
						this.edit(this.fundSysCode)
						this.$message({ type: 'success', message: '删除成功' })
					}
				})
			}
			this.cancelFile()
		},

		// 文件查看
		fileSee(id) {
			openFile(id)
		},

		// 文件下载
		downloadFileFunc(id) {
			downloadFile({ fid: id }).then(item => {
				if (item.msgCode === 'N') {
					this.$message({ showClose: true, message: '文件不存在', type: 'error' })
					return
				}
				if (item) {
					window.open(`/update/download.do?fid=${id}`)
					this.$message({ showClose: true, message: '正在下载，请稍等......', type: 'success' })
				}
			})
		},

		// 文件操作组件
		cancelFile() {
			this.addComShowFile = !this.addComShowFile
			if (!this.addComShowFile) {
				this.clearData()
			}
		},

		// 保存
		save(title, obj) {
			(obj.fundSysCode = this.fundSysCode), (obj.fundRaiseType = '1')
			if (title.includes('文件上传')) {
				saveUpdateFile({ fileRenewInfo: obj, fundSysCode: this.fundSysCode, fileRenewId: obj.fileRenewId }).then(res => {
					if (res.msgCode === 'Y') {
						this.edit(this.fundSysCode)
					}
				})
			} else if (title.includes('文件更新')) {
				obj.fileRenewId = this.fileObj.fileRenewId
				UpdateFile({ fileRenewInfo: obj, fundSysCode: this.fundSysCode, fileRenewId: obj.fileRenewId }).then(res => {
					if (res.msgCode === 'Y') {
						this.edit(this.fundSysCode)
					}
				})
			}
		},

		fileClick(obj, type, index) {
			// 操作类型
			this.fileObj = jsonArray(obj)
			this.fileOperType = type
			if (index) {
				this.tabIndex = index
			}
			this.cancelFile()
		},

		updateShow(title, obj, index) {
			// 上传组件显隐
			if (obj) {
				this.fileObj = jsonArray(obj)
				this.tabIndex = index
			}
			this.title = obj ? '文件更新' : `${title}-文件上传`
			this.updateCancel()
		},

		updateCancel() {
			this.updateAddComShow = !this.updateAddComShow
		},

		// 清空选项
		clearData() {
			this.fileTypeMsg = ''
			this.fileObj = {}
			this.title = ''
			this.fileList = []
			this.tabIndex = null
			this.fileOperType = ''
		},

		// 上传
		toUploadFile(params) {
			fundFileUpload(params).then(res => {
				if (res.msgCode === 'Y') {
					let fileUpload = {}
					if (this.tabIndex) {
						this.fileRenewApplyList.forEach((item, index) => {
							if (this.tabIndex - 1 === index) {
								const obj = this.fileParams(this.fileTypeMsg, res.obj, this.date1 ? this.date1 : new Date())
								fileUpload = { ...item, ...obj }
							}
						})
					} else {
						let fileRenewCode = ''
						this.listMsgPublic.forEach(item => {
							if (this.title.includes(item.title)) {
								fileRenewCode = item.fileRenewCode
							}
						})
						const item = {
							fundSysCode: this.fundSysCode,
							fileRenewCode
						}
						const obj = this.fileParams(this.fileTypeMsg, res.obj, this.date1 ? this.date1 : new Date())
						fileUpload = { ...obj, ...item }
					}
					this.$message({ type: 'success', message: `${this.title}-${this.fileTypeMsg}-文件上传成功` })
					this.save(this.title, fileUpload)
					this.updateCancel()
					this.clearData()
				}
			})
		},

		updateClick(title) {
			// 上传类型
			this.fileTypeMsg = title
		},

		cancel() {
			this.$emit('fileComIsShow')
			this.clearData()
		},

		// 新增或编辑
		addConfirm() {},

		edit(id) {
			this.fundSysCode = id
			getFundFile({ fundSysCode: id, fundRaiseType: '1' }).then(res => {
				if (res.msgCode === 'Y') {
					this.fileRenewApplyList = res.data.body.filter(item => !!item.firstFileId || !!item.sealedFileId || !!item.finalFileId)
				}
			})
		},

		fileParams(type, obj, date) {
			if (type === '盖章版') {
				return {
					sealedEffectiveDate: timerConversionDate(date), // 生效日期
					sealedFileId: obj.fid, // 文件ID
					sealedTDate: timerConversionDate(new Date()), // 上传日期
					sealedFileName: obj.fname, // 文件名
					sealedFileMd5: obj.fmd5 // 文件fmd5
				}
			} else if (type === 'word版') {
				return {
					firstEffectiveDate: timerConversionDate(date),
					firstFileId: obj.fid,
					firstTDate: timerConversionDate(new Date()),
					firstFileName: obj.fname, // 文件名
					firstFileMd5: obj.fmd5 // 文件fmd5
				}
			} else if (type === 'PDF版') {
				return {
					finalEffectiveDate: timerConversionDate(date),
					finalFileId: obj.fid,
					finalTDate: timerConversionDate(new Date()),
					finalFileName: obj.fname, // 文件名
					finalFileMd5: obj.fmd5 // 文件fmd5
				}
			}
		}
	}
}
</script>
<style lang="scss" scoped >
.updateClass {
	display: flex;
}
.updateClass > div {
	margin-right: 16px;
}
::v-deep td {
	padding: 12px !important;
}
</style>
